﻿
namespace DBFile.Core.Web
{
	public static class Extensions
	{
		public static void SaveFilesToDB(this System.Web.HttpRequest request)
		{
			if (request.Files.Count > 0)
			{
				for (int i = 0; i < request.Files.Count; i++)
				{
					System.Web.HttpPostedFile file = request.Files[i];
					if (file.FileName.Length > 0)
					{
						DBFile.Save(file);
					}
				}
			}
        }

        public static void SaveToDB(this System.Web.HttpPostedFileBase file)
        {
            DBFile.Save(file);
        }

        public static void SaveToDB(this System.Web.HttpPostedFileBase file, string fileName)
        {
            DBFile.Save(file, fileName);
        }

        public static void SaveToDB(this System.Web.HttpPostedFileBase file, bool overwrite)
        {
            DBFile.Save(file, overwrite);
        }

        public static void SaveToDB(this System.Web.HttpPostedFileBase file, string fileName, bool overwrite)
        {
            DBFile.Save(file, fileName, overwrite);
        }

		//public static void SaveFilesToDB(this System.Web.UI.Page page)
		//{
		//    page.AddOnPreRenderCompleteAsync(new BeginEventHandler(page.BeginSaveFiles),
		//        new EndEventHandler(page.EndSaveFiles));
		//}

		//public static IAsyncResult BeginSaveFiles(this System.Web.UI.Page page, object sender, EventArgs e, AsyncCallback cb, object state)
		//{
		//    if (page.Request.Files.Count > 0)
		//    {
		//        for (int i = 0; i < page.Request.Files.Count; i++)
		//        {
		//            System.Web.HttpPostedFile file = page.Request.Files[i];
		//            if (file.FileName.Length > 0)
		//            {
		//                DBFile.Save(file);
		//            }
		//        }
		//    }
			
		//}
		
		//public static void EndSaveFiles(this System.Web.UI.Page page, IAsyncResult ar)
		//{

		//}


	}
}
